layui.use(['table', 'jquery', 'layer', 'upload'], function () {
    var table = layui.table,
        layer = layui.layer,
        upload = layui.upload,
        $ = layui.jquery;

    // 表格加载
    var tableIns = table.render({
        elem: '#sale_basic_list', //绑定table id
        height: 'full-125',
        url: ctx + '/sale/basic_list',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers"}
            , {field: 'customerName', title: '公司名称'}
            , {field: 'productName', title: '产品'}
            , {field: 'productCount', title: '数量'}
            , {field: 'productUnitPrice', title: '单价（万元）'}
            , {
                field: 'empBean', title: '制单人', templet: function (data) {
                    return data.empBean.empName;
                }
            }
            , {field: 'createTime', title: '创建日期'}
            , {field: 'deliveryDate', title: '交货日期'}
            , {field: 'description', title: '文件'}
            , {
                field: 'status', title: '状态', templet: function (data) {
                    if (data.status == 0) {
                        return "待上传"
                    } else if (data.status == 1) {
                        return "待审批"
                    } else if (data.status == 2) {
                        return "审批完成"
                    } else if (data.status == 3) {
                        return "请修改"
                    }
                }
            },
            {
                title: "操作", width: 150, sort: true, fixed: "right", templet: function (data) {
                    if (data.status == 0) {
                        return '<button class="layui-btn layui-btn-primary layui-btn-xs upload_btn" value="' + data.id + '">上传</button>' +
                            '<button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="update" >修改</button> ';
                    } else if (data.status == 1 || data.status == 3) {
                        return '<button class="layui-btn layui-btn-xs upload_btn" value="' + data.id + '">替换</button>' +
                            '<a class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                            '  value=" ' + data.id + '  "  >下载</a>'
                            + '<button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="update" >修改</button> '
                    } else if (data.status == 2) {
                        return '<button class="layui-btn  layui-btn-warm   layui-btn-xs " style="margin-left: 2px" lay-event="downloadFile"' +
                            '  value=" ' + data.id + '  "  >下载</button>'
                    }
                }
            }
        ]],
        done: function (data, curr, count) {

            var tableId = this.id;
            var tableElem = this.elem;
            var tableViewElem = tableElem.next();
            var addBtn = tableViewElem.find('.upload_btn')
            upload.render({
                elem: addBtn,
                url: ctx + '/file/uploadFile',
                accept: 'file',
                auto: true,
                multiple: true,
                before: function (obj) {
                    var tableElem = this.item;
                    uploadParm = tableElem.attr('value');
                    layer.load();
                },
                data: {
                    id: function () {
                        return uploadParm;
                    }
                },
                done: function (res, index, upload) {
                    if (res.code == '0' && res.msg == "success") {
                        layer.msg(res.msg, {time: 1000})
                        layer.closeAll('loading')
                        tableIns.reload();
                    }
                },
                error: function (res, index) {
                    layer.msg(res.msg)
                }
            })

        }


    })
    // 监听头部工具栏
    table.on('toolbar(basicSaleFilter)', function (data) {
        if (data.event == "grant") {
            var checkStatus = table.checkStatus(data.config.id);
            openGrantBasicOrderDialog(checkStatus.data);
        }

        function openAddBasicDialog() {
            var url = ctx + "/sale/toAddBasicPage";
            layer.open({
                type: 2,
                title: "<h3>销售模块--- 提交报价单 </h3>",
                area: ['900px', '600px'],
                content: url,
                maxmin: true,
                shadeClose: true
            })
        }

        if (data.event == "add") {
            openAddBasicDialog();
        }
    })

    function openGrantBasicOrderDialog(data) {

        if (data.length == 0) {
            layer.msg("请选择审评订单", {icon: 6})
            return;
        }

        if (data.length > 1) {
            layer.msg("不能批量授权", {icon: 6})
            return;
        }
        if (data[0].status == 2){
            layer.msg("审批已经完成", {icon: 6})
            return;
        }

        layer.open({

            type: 2,
            title: "<h3>审批报价单</h3>",
            area: ['900px', '600px'],
            content: ctx + "/sale/toApprovePage?basicId=" + data[0].id,
            maxmin: true,
            shadeClose: true

        })

    }

    // 监听行工具栏
    table.on('tool(basicSaleFilter)', function (data) {
        if (data.event == "downloadFile") {
            window.location.href = ctx + "/file/downloadFile?id=" + data.data.id;
        } else if (data.event == "update") {
            openUpdateBasicDialog(data.data.id)

        }
    })

    function openUpdateBasicDialog(id) {
        var url;
        if (id != null && id != '') {
            var title = "<h3>组织架构 ----  更新报价单信息</h3>";
            url = ctx + "/sale/toUpdateBasicPage?id=" + id;
        }

        layer.open({
            type: 2,
            title: title,
            area: ['700px', '500px'],
            content: url,
            shadeClose: true,
            maxmin: true
        })
    }
})